package com.yoyoyo666.report.common;

import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateUtils {

    public final static DateFormat sdf = new MyDateFormat("yyyy-MM-dd HH:mm:ss");
    public final  static DateFormat sdf2 = new MyDateFormat("yyyy-MM-dd");
    public final  static DateFormat sdf1 = new MyDateFormat("yyyyMMdd");


    public static String getNowString(){
        return sdf.format(new Date());
    }
    public static String getNowStringShort(){
        return sdf2.format(new Date());
    }


    private static String work = "20181229,20190202,20190203,20190427,20190428,20190928,20190929";
    private static String xx = "20181231,20190101,20190204,20190205,20190206,20190207,20190208,20190405,20190429,20190430,20190501,20190607,20190913" +
            ",20191001,20191002,20191003,20191004,20191007";

    public static boolean isWork(Date date){
        String format = sdf1.format(date);
        if(work.indexOf(format)>-1){
            return true;
        }

        if(xx.indexOf(format)>-1){
            return false;
        }

        Calendar c = Calendar.getInstance();
        c.setTime(date);

        int i = c.get(Calendar.DAY_OF_WEEK);

        if(i==Calendar.SATURDAY||i==Calendar.SUNDAY){
            return false;
        }

        System.out.println(i);

        return true;
    }

    public static Date getDayStart(){
        Date date=new Date();//取时间
        Calendar c  = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY,0);
        c.set(Calendar.MINUTE,0);
        c.set(Calendar.SECOND,0);
        c.set(Calendar.MILLISECOND,0);
        return c.getTime();
    }

    public static Date getDayEnd(){
        Calendar c  = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY,23);
        c.set(Calendar.MINUTE,59);
        c.set(Calendar.SECOND,59);
        c.set(Calendar.MILLISECOND,999);
        return c.getTime();
    }


    public static void main(String[] args) throws ParseException {
        System.out.println(isWork(sdf1.parse("20190404")));
    }



    public static Date parse(String time){

        Date date = null;

        if(StringUtils.isBlank(time)){
            return null;
        }

        try {
            date = sdf2.parse(time);
        } catch (ParseException e) {
            try {
                date = sdf1.parse(time);
            } catch (ParseException e1) {
                try {
                    date = sdf.parse(time);
                } catch (ParseException e2) {
                    System.err.println("时间转化错误" + time);
                    e2.printStackTrace();
                }
            }
        }
        return date;
    }


}

class MyDateFormat extends SimpleDateFormat{
    public MyDateFormat(String pattern) {
        super(pattern);
        this.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
    }
}